package com.njwt.cloud.msconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author <a href="mailto:xingxiao@gtmap.cn">xingxiaofeng</a>
 * @version2.1 2022-04-24
 * @description
 */
@RestController
public class CUserController {
    @Autowired
    public LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getUser")
    public String getUser(){
     return restTemplate.getForObject(getUrl()+"/getUser",String.class );
    }

    private String getUrl(){
        ServiceInstance si=loadBalancerClient.choose("orders-provider");
        StringBuilder url=new StringBuilder();
        url.append("http://");
        url.append(si.getHost());
        url.append(":").append(si.getPort());
        //发远进行远程访问的url
        System.out.println("url======="+url.toString());
        return url.toString();

    }
}
